﻿using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
  public  class SYS_Element_Ruleword_RelDAL
    {
        private string DbName;
        public SYS_Element_Ruleword_RelDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYS_Element_Ruleword_Rel model)
        {
            serverhelper sqlserverhelper = new serverhelper("");
            string primaryKey = sqlserverhelper.CreateKeyID();
            string sql = string.Format(@"insert into SYS_Element_Ruleword_Rel(
			EW_ID
          ,CC_ID
          ,Ruleword
          ,Stopword,Rule_Synforbidword)
			values ('{0}','{1}','{2}','{3}','{4}')", primaryKey, model.CC_ID, model.Ruleword, model.Stopword,model.Rule_Synforbidword);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string swID)
        {
            string sql = string.Format(@" delete from SYS_Element_Ruleword_Rel where EW_ID='{0}' ", swID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取规则词信息
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetSysElementWord(string ccid="")
        {
            string sql = string.Empty;
            if (!string.IsNullOrEmpty(ccid))
            {
                sql = string.Format(@" select a.EW_ID, a.Ruleword,a.Stopword,a.CC_ID,b.CC_Name,case a.Lock when 1 then 'true' else 'false' end as Lock,a.Rule_Synforbidword,b.XmlPath from SYS_Element_Ruleword_Rel a join SYS_CaseCause b on a.CC_ID=b.CC_ID
where a.CC_ID in(select CC_ID from SYS_CaseCause where CHARINDEX(',{0},', ',' + AllParentId + ',') > 0) order by CC_ID  ", ccid);
            }
            else 
            {
                sql = " select * from SYS_Element_Ruleword_Rel ";
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }


        /// <summary>
        /// 修改筛除词
        /// </summary>
        public bool UpdateName(SYS_Element_Ruleword_Rel model)
        {
            string sql = string.Format(@" update SYS_Element_Ruleword_Rel set Ruleword='{0}',Stopword='{1}',Rule_Synforbidword='{2}' where EW_ID='{3}' ", model.Ruleword, model.Stopword,model.Rule_Synforbidword, model.EW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改锁定状态
        /// </summary>
        public bool UpdateElementLock(string ewId,bool lock1)
        {
            string sql = string.Format(@" update SYS_Element_Ruleword_Rel set Lock='{0}' where EW_ID='{1}' ", lock1==true?1:0, ewId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改规则词
        /// </summary>
        public bool UpdateElementWord(SYS_Element_Ruleword_Rel model)
        {
            string sql = string.Format(@" update SYS_Element_Ruleword_Rel set Ruleword='{0}',Rule_Synforbidword='{1}' where EW_ID='{2}' ", model.Ruleword, model.Rule_Synforbidword, model.EW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


    }
}
